export interface ValidationResult {
  isValid: boolean;
  message: string;
}

export const validatePassword = (password: string): ValidationResult => {
  if (!password || password.length < 8) {
    return {
      isValid: false,
      message: '密码长度必须至少为8个字符'
    };
  }

  if (!/(?=.*[A-Z])/.test(password)) {
    return {
      isValid: false,
      message: '密码必须包含至少一个大写字母'
    };
  }

  if (!/(?=.*[a-z])/.test(password)) {
    return {
      isValid: false,
      message: '密码必须包含至少一个小写字母'
    };
  }

  if (!/(?=.*\d)/.test(password)) {
    return {
      isValid: false,
      message: '密码必须包含至少一个数字'
    };
  }

  if (!/(?=.*[!@#$%^&*(),.?":{}|<>])/.test(password)) {
    return {
      isValid: false,
      message: '密码必须包含至少一个特殊字符'
    };
  }

  return {
    isValid: true,
    message: '密码格式正确'
  };
};

export const validateEmail = (email: string): ValidationResult => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  
  if (!email) {
    return {
      isValid: false,
      message: '邮箱不能为空'
    };
  }

  if (!emailRegex.test(email)) {
    return {
      isValid: false,
      message: '邮箱格式不正确'
    };
  }

  return {
    isValid: true,
    message: '邮箱格式正确'
  };
};

export const validateUsername = (username: string): ValidationResult => {
  if (!username || username.length < 2) {
    return {
      isValid: false,
      message: '用户名长度必须至少为2个字符'
    };
  }

  if (username.length > 50) {
    return {
      isValid: false,
      message: '用户名长度不能超过50个字符'
    };
  }

  if (!/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(username)) {
    return {
      isValid: false,
      message: '用户名只能包含字母、数字、下划线和中文'
    };
  }

  return {
    isValid: true,
    message: '用户名格式正确'
  };
};

export const validators = {
  validatePassword,
  validateEmail,
  validateUsername
};

export default validators;
